#include  "../../bootloader/linkage.h"

R15	=	0x00
R14	=	0x08
R13	=	0x10
R12	=	0x18
R11	=	0x20
R10	=	0x28
R9	=	0x30
R8	=	0x38
RBX	=	0x40
RCX	=	0x48
RDX	=	0x50
RSI	=	0x58
RDI	=	0x60
RBP	=	0x68
DS	=	0x70
ES	=	0x78
RAX	=	0x80
FUNC	=	0x88
ERRCODE	=	0x90
RIP	=	0x98
CS	=	0xa0
RFLAGS	=	0xa8
OLDRSP	=	0xb0
OLDSS	=	0xb8


ENTRY(kernel_thread_func)
	popq	%r15	
	popq	%r14		
	popq	%r13		
	popq	%r12		
	popq	%r11		
	popq	%r10		
	popq	%r9		
	popq	%r8		
	popq	%rbx	
	
	popq	%rcx		
	popq	%rdx		
	popq	%rsi		
	popq	%rdi		
	popq	%rbp		
	popq	%rax		
	movq	%rax,	%ds	
	popq	%rax		
	movq	%rax,	%es	
	popq	%rax		
	addq	$0x38,	%rsp	
	movq	%rdx,	%rdi	
	callq	*%rbx
			
	movq	%rax,	%rdi
	callq	do_exit
	retq




ENTRY(system_call)
	sti
	subq	$0x38,	%rsp			 
	cld;					 

	pushq	%rax;				 	
	movq	%es,	%rax;			 	
	pushq	%rax;				 	
	movq	%ds,	%rax;			 	
	pushq	%rax;				 	
	xorq	%rax,	%rax;			 	
	pushq	%rbp;				 	
	pushq	%rdi;				 	
	pushq	%rsi;				 	
	pushq	%rdx;				 	
	pushq	%rcx;				 
	pushq	%rbx;				 	
	pushq	%r8;				 	
	pushq	%r9;				 	
	pushq	%r10;				 
	pushq	%r11;				 
	pushq	%r12;				 	
	pushq	%r13;				 
	pushq	%r14;				 	
	pushq	%r15;				 	
	movq	$0x10,	%rax;			 	
	movq	%rax,	%ds;			 	
	movq	%rax,	%es;			 
	movq	RAX(%rsp),	%rax	
	movq   %rsp,%rdi		 	
	callq	_ZN17systemcallmanager16HandleSystemCallEP10StackFrame		 	////////
	movq	%rax,	RAX(%rsp)

ENTRY(ret_system_call)
			 
	popq	%r15				 
	popq	%r14				 	
	popq	%r13				 	
	popq	%r12				 	
	popq	%r11				 	
	popq	%r10				 	
	popq	%r9				 	
	popq	%r8				 	
	popq	%rbx				 	
	popq	%rcx				 	
	popq	%rdx				 	
	popq	%rsi				 	
	popq	%rdi				 	
	popq	%rbp				 	
	popq	%rax				 	
	movq	%rax,	%ds			 
	popq	%rax				 
	movq	%rax,	%es			 
	popq	%rax
	
	addq	$0x38,	%rsp	
	xchgq 	%rdx,%r10
	xchgq  	%rcx,%r11
	sti	
	.byte	0x48		 
	sysexit	




ENTRY(user_level_function)
	pushq %rbp
	movq %rsp, %rbp
	subq $0x10, %rsp
	movq userFunctionAddr(%rip),%rcx
	call *%rcx
	addq $0x10, %rsp
	popq %rbp
	retq

userFunctionAddr:
	.quad userFunction